package org.test.ping.Utils;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;

/**
 * @ClassName RateLimiterUtil
 * @Description TODO
 * @Author JiaLin
 * @Date 2024/11/20 20:15
 */
public class RateLimiterUtil {
    public static boolean tryLock(String fileName, Long maxCount)
    {
        boolean flag = false;
        FileLock lock = null;
        FileChannel channel = null;
        try{
            RandomAccessFile file = new RandomAccessFile(fileName, "rw");
            channel = file.getChannel();
            lock= channel.tryLock();
            if (lock != null){
                flag = true;
            }
          } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (lock != null){
                try {
                    lock.release();
                    lock = null;
                    channel.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }
}
